<?php

// $Id: user_md5pwd_dbc.class.php 1571 2009-01-13 13:08:33Z ogrebg $

require_once('user_dbc.class.php');


class User_Md5Pwd_DBC extends User_DBC {
	private $db_table_prefix;
	private $password_clear_form = true; //indicates if $password is in clear form or some already hashed form
	

	function __construct($db_table_prefix = '') {
		$this->set_db_table_prefix($db_table_prefix);
	}

	
	public function set_password($password, $clear_form = true) {
		parent::set_password($password);
		$this->set_password_clear_form($clear_form);
	}
	

	public function set_db_table_prefix($db_table_prefix) {
		$this->db_table_prefix = $db_table_prefix;
	}


	public function get_db_table_prefix() {
		return $this->db_table_prefix;
	}


	public function set_password_md5($password_md5) {
		$this->password_md5 = $password_md5;
	}


	public function get_password_md5() {
		return $this->password_md5;
	}


	public function set_password_clear_form($password_clear_form) {
		$this->password_clear_form = $password_clear_form;
	}
	
	
	public function load_by_id(DB_Connection $dbc, $id) {
		global $_MODULES_REGISTER;

		$ret = false;

		$sql = "select username, password, disabled from ".$this->get_db_table_prefix()."users where id = $id";
		$rez = $dbc->execute($sql);

		if (!$rez->is_eof()) {
			$rez_obj = $rez->fetch_object();
			$this->set_id($id);
			$this->set_username($rez_obj->USERNAME);
			$this->set_password($rez_obj->PASSWORD, false);
			$this->set_disabled($rez_obj->DISABLED);

			$ret = $id;
		}

		return $ret;
	}


	protected function insert(DB_Connection $dbc) {
		$ret = false;

		$id = $dbc->generate_id($this->get_db_table_prefix()."users_seq");
		if ($id) {

			$username = addslashes($this->get_username());
			if ($this->get_password_clear_form()) {
				$password = md5($this->get_password());
				$this->set_password($password, false);
			} else {
				throw Tangra_Module_Exception("Password is not in clear form. Cannot md5 hash because will create a mess.");
			}
			$disabled = $this->get_disabled();

			$sql = "insert into ".$this->get_db_table_prefix()."users ".
							"(id, ".
							"username, ".
							"password, ".
							"disabled) ".
							"values ".
							"($id, ".
							"'$username', ".
							"'$password', ".
							$disabled.") ";

			$dbc->execute($sql);
			$ret = $id;
		} else {
			throw new TE_Exception("ID not generated - ".$this->get_db_table_prefix()."users_seq");
		}

		return $ret;
	}


	protected function update(DB_Connection $dbc) {
		$username = addslashes($this->get_username());
		if ($this->get_password_clear_form()) {		
			$password = md5($this->get_password());
			$this->set_password($password, false);			
		} else {
			throw Tangra_Module_Exception("Password is not in clear form. Cannot md5 hash because will create a mess.");
		}
		
		$disabled = $this->get_disabled();

		$sql = "update ".$this->get_db_table_prefix()."users set ".
						"username = '$username', ".
						"password = '$password', ".
						"disabled = $disabled ".
					"where id = ".$this->get_id();

		$dbc->execute($sql);

		return $this->get_id();
	}


	public static function check_login(DB_Connection $dbc, $username, $password, $prefix = '') {
		$ret = false;

		$sql = "select id from ".$prefix."users where username = '".$username."' and password = '".md5($password)."' and disabled = 0";

		$rez = $dbc->execute($sql);
		if (!$rez->is_eof()) {
			$rez_obj = $rez->fetch_object();
			$ret = new User_Md5Pwd_DBC($prefix);
			$ret->load_by_id($dbc, $rez_obj->ID);
		}

		return $ret;
	}



	public static function get_sql_for_grid() {
		$sql = "select id, username, disabled from users ";

		return $sql;
	}


	public static function get_sql_count_for_grid() {
		$sql = "select count(id) as total_rows from users ";

		return $sql;
	}
}